package com.ycy.chuwenuniapp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycy.chuwenuniapp.common.Constant;
import com.ycy.chuwenuniapp.common.ResCodeEnum;
import com.ycy.chuwenuniapp.common.ResData;
import com.ycy.chuwenuniapp.dto.GoodsColorDto;
import com.ycy.chuwenuniapp.entity.GoodsColor;
import com.ycy.chuwenuniapp.entity.GoodsCount;
import com.ycy.chuwenuniapp.exception.CustomException;
import com.ycy.chuwenuniapp.mapper.IGoodsColorMapper;
import com.ycy.chuwenuniapp.mapper.IGoodsCountMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
public class GoodsCountService extends ServiceImpl<IGoodsCountMapper, GoodsCount> {


    public Boolean saveGoodsCount(String type, GoodsCount entity) {
        String colorId = entity.getColorId();
        Integer count = entity.getCount();
        if(Constant.ADD.equals(type) && StrUtil.isBlank(colorId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失商品颜色id");
        if(Constant.UPDATE.equals(type) && StrUtil.isBlank(entity.getId())) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失商品数量id");
        if(count == null) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失尺码对应的颜色的商品数量");

//        QueryWrapper<GoodsColor> wrapper = new QueryWrapper<>();
//        wrapper.eq("goods_id",goodsId);
//        wrapper.eq("color",color);
//        long sameCount = count(wrapper);
//        if(sameCount != 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"该商品已存在相同的颜色: " + color);

        if(Constant.ADD.equals(type)) return save(entity);
        else if(Constant.UPDATE.equals(type)) return updateById(entity);
        else throw new CustomException(ResCodeEnum.BAD_REQUEST,"saveGoodsCount方法的类型值只能是Constant.ADD或Constant.UPDATE");
    }

    public List<GoodsCount> findAllEmptyCount() {
        QueryWrapper<GoodsCount> wrapper = new QueryWrapper<>();
        wrapper.eq("count",0);
        return list(wrapper);
    }

    //查全部
    public List<GoodsCount> findAllByIds(List<String> countIdList) {
        QueryWrapper<GoodsCount> wrapper = new QueryWrapper<>();
        wrapper.in("id",countIdList);
        return list(wrapper);
    }

    public List<GoodsCount> findAllBySizeAndColorIds(ArrayList<String> sizeIdList,ArrayList<String> colorIdList) {
        QueryWrapper<GoodsCount> wrapper = new QueryWrapper<>();
//        wrapper.in("size_id",sizeIdList);
        wrapper.in("color_id",colorIdList);
//        wrapper.ne("count",0);
        return list(wrapper);
    }

    public Boolean delete(List<String> colorIdList) {
        QueryWrapper<GoodsCount> wrapper = new QueryWrapper<>();
        wrapper.in(colorIdList.size() != 0,"color_id",colorIdList);
        return remove(wrapper);
    }

//    public ResData<Boolean> delete(String id) {
//        if(StrUtil.isBlank(id)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失商品颜色id");
//        boolean isRemove = removeById(id);
//        if(isRemove) return ResData.success(true,"删除成功");
//        else return ResData.fail(false,"不存在该id的商品尺码或服务器错误");
//    }
}
